<?php

class Application_Model_UserMapper
{
	protected $_dbTable;

	public function setDbTable($dbTable)
	{
		if (is_string($dbTable)) {
			$dbTable = new $dbTable();
		}
		if (!$dbTable instanceof Zend_Db_Table_Abstract) {
			throw new Exception('Invalid table data gateway provided');
		}
		$this->_dbTable = $dbTable;
		return $this;
	}

	public function getDbTable()
	{
		if (null === $this->_dbTable) {
			$this->setDbTable('Application_Model_DbTable_User');
		}
		return $this->_dbTable;
	}

	public function save(Application_Model_User $user)
	{
		$data = array(
				'email'				=> $user->getEmail(),
				'firstName' 		=> strtolower($user->getFirstName()),
				'lastName'			=> strtolower($user->getLastName()),
				'password'			=> sha1($user->getPassword()),
				'sex'				=> $user->getSex(),
				'country'			=> $user->getCountry(),
				'birthday'			=> $user->getYear()."-".$user->getMonth()."-".$user->getDay(),
				'token'				=> sha1($user->getEmail().$_SERVER['REMOTE_ADDR'].$user->getPassword()),
				'ip_registration'	=> $_SERVER['REMOTE_ADDR'],
				'status'			=> 0,
				'created' 			=> date('Y-m-d H:i:s'),
				'mobile'			=> $user->getMobile(),
				'avatar'			=> $user->getAvatar(),
				
				
		);

		if (null === ($id = $user->getUserId())) {
			unset($data['id_user']);
			return $this->getDbTable()->insert($data);
		} else {
			$this->getDbTable()->update($data, array('id_user = ?' => $id));
			return $id;
		}
	}
	
	public function edit(Application_Model_User $user){

		$data = array(
				'firstName' 		=> strtolower($user->getFirstName()),
				'lastName'			=> strtolower($user->getLastName()),
				'sex'				=> $user->getSex(),
				'mobile'			=> $user->getMobile(),
		);
		
		if (null === ($id = $user->getUserId())) {
			unset($data['id_user']);
			return $this->getDbTable()->insert($data);
		} else {
			$this->getDbTable()->update($data, array('id_user = ?' => $id));
			return $id;
		}
	}
	
	public function editPassword(Application_Model_User $user){
	
		$data = array(
				'password' 			=> $user->getPassword(),
		);
	
		if (null === ($id = $user->getUserId())) {
			unset($data['id_user']);
			return $this->getDbTable()->insert($data);
		} else {
			$this->getDbTable()->update($data, array('id_user = ?' => $id));
			return $id;
		}
	}
	
	public function validateUserByToken($token)
	{
		$where = array('token = ?' => (int)$token);
		$this->getDbTable()->update(array('status' => 1), $where);
		//return $user;
	}
	
	public function updateAvatar($id_user,$avatar)
	{
		$where = array('id_user = ?' => $id_user);
		$this->getDbTable()->update(array('avatar' => $avatar), $where);
		//return $user;
	}

	public function find($id)
	{
		$result = $this->getDbTable()->find($id);
		if (0 == count($result)) {
			return;
		}
		$row = $result->current();
		$user = new Application_Model_User();
		$user->setUserId($row->id_user);
		$user->setEmail($row->email);
		$user->setFirstName($row->firstName);
		$user->setLastName($row->lastName);
		$user->setPassword($row->password);
		$user->setSex($row->sex);
		$user->setCountry($row->country);
		$user->setMobile($row->mobile);
		$user->setAvatar($row->avatar);
		$user->setBirthday($row->birthday);
		
		
		return $user;
	}

	
	public function fetchAll($where, $order = null, $count = null, $offset = null){
		$rows = $this->getDbTable()->fetchAll($where,$order,$count,$offset);
		if (0 == count($rows)) {
			return null;
		}
		$users = array();
	
		foreach ($rows as $row){
			$user = new Application_Model_User();
			$user->setUserId($row->id_user);
			$user->setEmail($row->email);
			$user->setFirstName($row->firstName);
			$user->setLastName($row->lastName);
			$user->setPassword($row->password);
			$user->setSex($row->sex);
			$user->setCountry($row->country);
			$user->setMobile($row->mobile);
			$user->setAvatar($row->avatar);
			$user->setBirthday($row->birthday);
			$users[] = $user;
		}
	
		return $users;
	}
}
